package cn.toutatis.sandbox.root.request;

import cn.toutatis.common.exception.UnableToResolvedException;
import cn.toutatis.common.result.Ignore;
import cn.toutatis.common.result.Result;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@ControllerAdvice
public class RemoveResponseField implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body == null){ return null; }
        Class<?> aClass = body.getClass();
        if ( aClass == Result.class){
            Result result = (Result) body;
            Ignore annotation = returnType.getMethodAnnotation(Ignore.class);
            if (annotation != null){
                Object content = result.getContent();
                if (content == null){
                    throw new NullPointerException("result.content is null.");
                }
                String[] ignore = annotation.ignore();
                boolean required = annotation.required();
                Class<?> contentClass = content.getClass();
                if (List.class.isAssignableFrom(contentClass)){
//                    TODO 以后再解决list的问题
//                    JSONArray jsonArray = (JSONArray) JSON.toJSON(content);
                }else if (Map.class.isAssignableFrom(contentClass) || Object.class.isAssignableFrom(contentClass)){
                    JSONObject jsonObject = (JSONObject) JSON.toJSON(content);
                    removeKey(jsonObject,ignore,required);
                }
            }
            body = result;
        }
        return body;
    }

    private JSONObject removeKey(JSONObject o,String[] keys,boolean required){
        JSONObject object = o;
        for (String properties : keys) {
            String[] split = properties.split(".");
            List<String> strings = Arrays.asList(split);
            for (int i = 0; i < split.length; i++) {
                boolean exist = object.containsKey(split[i]);
                if (exist){
                    JSONObject inner = (JSONObject) JSON.toJSON(object.get(split[i]));
                }else {
                    if (required){
                        throw new UnableToResolvedException();
                    }
                }
            }

        }
        return object;
    }

    private void checkNext(List<String> list,int index,JSONObject jsonObject){

    }
}
